package com.ideal.gpt.ability.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.lzwork.utility.collection.CollUtility;

/**
 * @author LiZheng
 * @date 2023年9月22日 上午10:29:02
 */

public class NlpJob
{
	static Set<String> natureFilter = CollUtility.arrayToSet("c");
	static Set<String> natureCombine = CollUtility.arrayToSet("d");
	static String[] prefixFilter =
	{ "w", "ude" };

	public static List<String> splitCoreWords(String input)
	{
		return splitCoreWords(input, false);
	}

	public static List<String> splitCoreWords(String input, boolean skipOrigin)
	{
		List<String> seg = new ArrayList<>();

		List<Term> term = HanLP.segment(input);

		if (term.size() == 1 && skipOrigin)
			return seg;

		String last = null;

		term: for (Term t : term)
		{
			String nature = t.nature.toString();

			if (natureFilter.contains(nature))
			{
				last = null;
				continue term;
			}

			if (natureCombine.contains(nature))
			{
				if (last != null)
					last += t.word;
				else
					last = t.word;
				continue term;
			}

			for (String p : prefixFilter)
			{
				if (nature.startsWith(p))
				{
					last = null;
					continue term;
				}
			}

			String segWord = t.word;
			if (last != null)
				segWord = last + t.word;

			seg.add(segWord);
			last = null;
		}

		return seg;
	}
}
